﻿using System;
using MySql.Data.MySqlClient;

namespace MySql.Simple.Mapper.Attributes
{
    /// <summary>
    /// Attribute to specify what column a property in a class should be
    /// saved in.
    /// </summary>
    [AttributeUsage(AttributeTargets.Property)]
    public class ColumnAttribute : Attribute
    {
        private MySqlDbType type;
        private bool typeHasValue;

        /// <summary>
        /// Initializes a new instance of the <see cref="ColumnAttribute"/> class.
        /// </summary>
        public ColumnAttribute()
        {
            Length = 0;
            Nullable = true;

            typeHasValue = false;
        }

        /// <summary>
        /// Gets or sets the name of the column.
        /// </summary>
        /// <value>The name.</value>
        public string Name { get; set;}

        /// <summary>
        /// Gets or sets a value indicating whether this column is the primary key.
        /// </summary>
        /// <value><c>true</c> if this column is the primary key; otherwise, <c>false</c>.</value>
        public virtual bool PrimaryKey { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="ColumnAttribute"/> is assigned automatically from the database.
        /// This is used for auto-generated primary keys.
        /// </summary>
        /// <value><c>true</c> if assigned; otherwise, <c>false</c>.</value>
        public bool AutoIncrement { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="ColumnAttribute"/> is searchable.
        /// </summary>
        /// <value><c>true</c> if searchable; otherwise, <c>false</c>.</value>
        public bool Searchable { get; set; }

        /// <summary>
        /// Gets or sets the length.
        /// </summary>
        /// <value>The length.</value>
        public int Length { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="ColumnAttribute"/> is nullable.
        /// </summary>
        /// <value><c>true</c> if nullable; otherwise, <c>false</c>.</value>
        public bool Nullable { get; set; }

        /// <summary>
        /// Gets or sets the type.
        /// </summary>
        /// <value>The type.</value>
        public MySqlDbType Type
        {
            get { return type; }
            set 
            { 
                type = value;
                typeHasValue = true;
            }
        }

        /// <summary>
        /// Gets a value indicating whether <see cref="Type"/> has value.
        /// </summary>
        /// <value><c>true</c> if [type has value]; otherwise, <c>false</c>.</value>
        public bool TypeHasValue
        {
            get { return typeHasValue; }
        }
    }
}